home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4953 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. Path: mics.demon.co.uk!Bill
  2. From: Bill Michell <Bill@mics.demon.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question on these 2 constructor variations
  5. Date: Thu, 1 Feb 1996 20:23:33 +0000
  6. Organization: None
  7. Distribution: world
  8. Message-ID: <mtcFoGAFFSExEwaI@mics.demon.co.uk>
  9. References: <310a6256.34676691@nntp.ix.netcom.com>
  10. NNTP-Posting-Host: mics.demon.co.uk
  11. X-NNTP-Posting-Host: mics.demon.co.uk
  12. MIME-Version: 1.0
  13. X-Newsreader: Turnpike Version 1.11 <uiEOes1orVXTvvWgSAnAOBUy+p>
  14.  
  15. In article <310a6256.34676691@nntp.ix.netcom.com>, "Brian F. Haddock"
  16. <BHaddock@ix.netcom.com> writes
  17. >I'm really confused on something, and can't seem to find any details
  18. >on the following type of constructor.  Why does this type of
  19. >constructor work...
  20. >
  21. >CMyPropertySheet::CMyPropertySheet(LPCTSTR, CWnd* pParent, UINT)
  22. >       : CPropertySheet("Game Options", NULL, 0)
  23. >
  24. >while this one will not?
  25. >
  26. >CMyPropertySheet::CMyPropertySheet(LPCTSTR name, CWnd* pParent, UINT
  27. >tmpNum)
  28. >{
  29. >       name = "Game Options";
  30. >       pParent = NULL;
  31. >       tmpNum = 0;
  32. >}
  33. >
  34. >Ignoring any syntax errors I may have made typing this in, what is the
  35. >difference in using the ' : ' type of constructor (first example) and
  36. >the one that inits the fields between the brackets (second example)?
  37. >Are they just 2 different variations of doing the same thing?
  38.  
  39. No they are not identical, though they do appear to behave identically
  40. (if they work)...
  41.  
  42. The second version (without the colon) tries to call the default
  43. constructor of class CPropertySheet, and then change the default values
  44. stored therin. This works fine if a) a default constructor exists and b)
  45. the CMyPropertySheet object (or CPropertySheet object if it is a member
  46. of your class - though from the context, I suspect you have just derived
  47. your class from it) are not const.
  48.  
  49. The first (working) version does not rely on the presence of a default
  50. constructor - it uses whatever explicit form of construction you
  51. specify. It also uses an initialisation, not an assignment, and so works
  52. fine with const objects. In (almost) all real-world cases, it will be at
  53. least as fast as, and possibly faster than, initialising the object and
  54. immediately assigning new values to it.
  55.  
  56. You should always use the first form whenever possible, since it means
  57. people who re-use your class can declare const objects of this type,
  58. should they want to.
  59.  
  60. So to try and express myself a little more clearly, the key difference
  61. is that the second example does *not* init the fields in the body of
  62. your code - it inits the fields by calling the default constructor of
  63. the parent class (or contained object) and then assigns new values to
  64. the member variables.
  65. ---
  66. Bill Michell   eMail: Bill@mics.demon.co.uk
  67.